package com.javaear.beangenerator;

import com.javaear.beangenerator.tools.IOUtils;

/**
 * @author aooer
 */
public abstract class BeanGenerator {

    /*************
     * Create bean model file
     ************/

    /**
     * create java Bean file from mysql table
     *
     * @param tableName tableName
     */
    public static void createFromMysql(String tableName) {
        RenderHolder.getInstance().mysqlRender().render(tableName);
    }

    /**
     * create java Bean file from jsonString
     *
     * @param jsonString jsonString
     * @param beanName   beanName
     */
    public static void createFromJson(String jsonString, String beanName) {
        RenderHolder.getInstance().jsonRender(jsonString).render(beanName);
    }

    /**
     * create java Bean file from jaxb xml document
     *
     * @param jaxbXmlPath jaxbXmlPath
     */
    public static void createFromJaxb(String jaxbXmlPath) {
        RenderHolder.getInstance().jaxbRender().render(jaxbXmlPath);
    }

    /*************
     * Config the Create Action
     ************/

    /**
     * set code tempalte path
     *
     * @param codeTemplatePath code tempalte path
     */
    public static void setCodeTemplate(String codeTemplatePath) {
        try {
            String codeTemplateStr = IOUtils.getContent(codeTemplatePath);
            Context context = Configure.getInstance().getContext();
            context.put("config_code_template_copyright", codeTemplateStr.split("#copyright")[1].trim());
            context.put("config_code_template_class", codeTemplateStr.split("#class")[1].trim());
            context.put("config_code_template_getter", codeTemplateStr.split("#getter")[1].trim());
            context.put("config_code_template_setter", codeTemplateStr.split("#setter")[1].trim());
            context.put("config_code_template_enable", "true");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * set dest create dir
     *
     * @param destDir destDir
     */
    public static void setDestDir(String destDir) {
        Context context = Configure.getInstance().getContext();
        context.put("config_dest_dir", destDir);
    }

    /**
     * set package name
     *
     * @param packageName packagename
     */
    public static void setPackageName(String packageName) {
        try {
            Context context = Configure.getInstance().getContext();
            context.put("config_package_name", packageName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * set is innerClass style
     *
     * @param isInnerClassStyle is Or not
     */
    public static void setEmbedStyle(Boolean isInnerClassStyle) {
        Context context = Configure.getInstance().getContext();
        context.put("config_embed_style", isInnerClassStyle.toString());
    }

}
